using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace GameForKids
{
    class Animation
    {
        #region Fields
        Texture2D animatedCharacter;
        Point sheetSize;
        Point currentFrame;
        public Point FrameSize { get; set; }

        public int FrameCount
        {
            get { return sheetSize.X * sheetSize.Y; }
        }

        public Vector2 Offset { get; set; }


        public int FrameIndex
        {
            get
            {
                return sheetSize.X * currentFrame.Y + currentFrame.X;
            }
            set
            {
                if (value >= sheetSize.X * sheetSize.Y + 1)
                {
                    throw new InvalidOperationException( "vuot qua kich co sprite");
                }
                currentFrame.Y = value / sheetSize.X;
                currentFrame.X = value % sheetSize.X;
            }
        }

        public bool IsActive { get; private set; }

        public void Active()
        {
            IsActive = true;
        }
        public void Deactive()
        {
            IsActive = false;
        }

        #endregion

        #region Initialization

        public Animation(Texture2D frameSheet, Point size,Point frameSheetSize)
        {
            animatedCharacter = frameSheet;
            FrameSize = size;
            sheetSize = frameSheetSize;
            Offset = Vector2.Zero;
        }
        #endregion

        #region Update and Render
        public void Update()
        {
            if (IsActive)
            {
                if (FrameIndex >= FrameCount - 1)
                {
                    FrameIndex = 0;
                   // IsActive = false;
                }
                else
                {
                    currentFrame.X++;
                    if (currentFrame.X >= sheetSize.X)
                    {
                        currentFrame.X = 0;
                        currentFrame.Y++;
                    }
                    if (currentFrame.Y >= sheetSize.Y)
                        currentFrame.Y = 0;
                }
            }
        }


        public void Draw(SpriteBatch spriteBatch, Vector2 position,SpriteEffects spriteEffect, float scale, int alpha)
        {
            Draw(spriteBatch, position, scale, spriteEffect, alpha);
        }


        public void Draw(SpriteBatch spriteBatch, Vector2 position, float scale,SpriteEffects spriteEffect, int alpha)
        {
            spriteBatch.Draw(animatedCharacter, position + Offset, new Rectangle(
                  FrameSize.X * currentFrame.X,
                  FrameSize.Y * currentFrame.Y,
                  FrameSize.X,
                  FrameSize.Y),
                  new Color(alpha,alpha,alpha), 0f, Vector2.Zero, scale, spriteEffect, 0);
        }
        public void Draw(SpriteBatch spriteBatch, Vector2 position, float scale, Color color,SpriteEffects spriteEffect, int alpha)
        {
            spriteBatch.Draw(animatedCharacter, position + Offset, new Rectangle(
                  FrameSize.X * currentFrame.X,
                  FrameSize.Y * currentFrame.Y,
                  FrameSize.X,
                  FrameSize.Y),
                  new Color(alpha,alpha,alpha), 0f, Vector2.Zero, scale, spriteEffect, 0);
        }


        public void PlayFromFrameIndex(int frameIndex)
        {
            FrameIndex = frameIndex;
            IsActive = true;
        }

        public void updateFrameWidth(Point p)
        {
            FrameSize = p;
        }
        #endregion
    }
}
